/**
 * com.future CO.,ltd.
 */

package com.future.mq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;


/**
 * 类说明：
 */
public class Sender {

    public static void main(String[] args) throws Exception {

        // 获取连接工厂
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://localhost:61616"
        );
        // 获取一个向ActiveMQ的连接
        Connection connection = connectionFactory.createConnection();
        // 获取session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 找目的地，获取destination,消费端，也会从这个目的地取消息
        Queue queue = session.createQueue("user");

        // 向目的地写入消息
        MessageProducer producer = session.createProducer(queue);

        for (int i = 0; i < 100; i++) {
            // 创建消息
            TextMessage textMessage = session.createTextMessage("hi" + i);
            Thread.sleep(500);
            // 向目的地写入消息
            producer.send(textMessage);
        }
        // 关闭连接
        connection.close();
        System.out.println("发送成功");
    }
}
